using System;
using System.Text;

namespace CodeStory.SmartUpload
{
	internal class SmartProgress
	{
		private DateTime beginTime = DateTime.Now;
		private int contentLength = 0;
		private string currentFileName = string.Empty;
		private UploadStatus status = UploadStatus.Initializing;

		private int uploadedLength = 0;
		public int ContentLength {
			get { return contentLength; }
			set { contentLength = value; }
		}

		public int UploadedLength {
			get { return uploadedLength; }
			set { uploadedLength = value; }
		}

		public UploadStatus Status {
			get { return status; }
			set { status = value; }
		}

		public string CurrentFileName {
			get { return currentFileName; }
			set { currentFileName = value; }
		}

		public string Speed {
			get { return CaculateSpeed(); }
		}

		public string PercentString {
			get {
				int percent = 0;
				if (contentLength != 0) {
					percent = uploadedLength * 100 / contentLength;
				}
				return string.Format("{0}%", percent);
			}
		}

		private string CaculateSpeed() {
			TimeSpan span = DateTime.Now.Subtract(beginTime);
			double speed = (double)uploadedLength / span.Seconds;
			if (speed < 1024) {
				return string.Format("{0}Byte/S", speed);
			}
			if (speed < 1024 * 1024) {
				return string.Format("{0}KB/S", speed / 1024);
			}
			if (speed < 1024 * 1024 * 1024) {
				return string.Format("{0}MB/S", speed / 1024 / 1024);
			}
			return "0Byte/s";
		}

		public override string ToString() {
			StringBuilder sb = new StringBuilder();
			sb.Append("{").Append("TotalLength:").Append(ContentLength).Append(",UploadedLength:").Append(UploadedLength).Append(",Status:'").Append(Status).Append("',CurrentFile:'").Append(CurrentFileName).Append("',Speed:'").Append(Speed).Append("',Percent:'").Append(PercentString).Append("'}");
			return sb.ToString();
		}
	}

	public enum UploadStatus : byte
	{
		Initializing,
		Uploading,
		Error,
		UserAbort,
		Completed,
		FileMoving,
	}
}